package com.virtualCat.controller.bankend;

import com.virtualCat.common.Const;
import com.virtualCat.common.Msg;
import com.virtualCat.service.AdminService;
import com.virtualCat.service.UserService;
import com.virtualCat.vo.UserInfoDetailVo;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/manage/user/")
@ResponseBody
public class UserManageController {
    @Autowired
    UserService userService;
    @Autowired
    AdminService adminService;

    @RequestMapping(value = "login",method = RequestMethod.POST)
    public Msg login(HttpSession session,@RequestParam(value = "username") String username,
                     @RequestParam(value = "password")String password){
        if (adminService.checkLogin(username,password)>0){
            session.setAttribute(Const.admin_login,username);
            return Msg.success();
        }
        return Msg.fail().add("msg","relogin");
    }

    @RequestMapping(value = "logout",method = RequestMethod.GET)
    public Msg login(HttpSession session){
        session.removeAttribute(Const.admin_login);
        return Msg.success();
    }

    @RequestMapping(value = "findById",method = RequestMethod.GET)
    public Msg getUserInfo(HttpSession session,@RequestParam(value = "openid") String openid){
        String value = (String) session.getAttribute(Const.admin_login);
        if (value!=null){
            return userService.getUserInfoById(openid);
        }
        return Msg.fail().add("msg","login");
    }

    @RequestMapping(value = "searchUserInfoList",method = RequestMethod.GET)
    @ApiOperation(value="3.3.查询用户信息",httpMethod="GET",notes="查询用户信息")
    public Msg searchUserInfoList(HttpSession session,
                                  @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                  @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum){
        String value = (String) session.getAttribute(Const.admin_login);
        if (value!=null){
            return userService.getUserInfo(pageNum,pageSize);
        }
        return Msg.fail().add("msg","login");
    }

    @RequestMapping(value = "updateUserInfo",method = RequestMethod.POST)
    @ApiOperation(value="3.3.修改用户信息",httpMethod="POST",notes="修改用户信息")
    public Msg updateUserInfo(HttpSession session, @RequestBody UserInfoDetailVo userInfo){
        String value = (String) session.getAttribute(Const.admin_login);
        if (value!=null){
            return userService.updateUserInfo(userInfo);
        }
        return Msg.fail().add("msg","login");
    }

    @RequestMapping(value = "deleteUserInfo",method = RequestMethod.DELETE)
    @ApiOperation(value="3.3.删除用户信息",httpMethod="DELETE",notes="删除用户信息")
    public Msg deleteUserInfo(HttpSession session,String openid){
        String value = (String) session.getAttribute(Const.admin_login);
        if (value!=null){
            return userService.deleteUser(openid);
        }
        return Msg.fail().add("msg","login");
    }


}
